﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="apps.aspx.cs" Inherits="SampleSite.admin.apps" %>


<div>
    <table id="apps"></table>
</div>

<div id="dlgAddApp" class="easyui-dialog" style="width:320px;height:180px;padding:10px 20px"
		closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <form id="AddApp" method="post">
        <table>
            <tr>
                <th colspan="2" >添加应用</th>
            </tr>              
            <tr>
                <td><label for="AppNameInAddApp">应用名称：</label></td>
                <td><input class="easyui-validatebox" type="text" id="AppNameInAddApp" name="AppName" 
                            style="width:175px;"
                            data-options="required:true" /></td>
            </tr>
            <tr>
                <td><label for="AppUrlInAddApp">URL地址：</label></td>
                <td><input class="easyui-validatebox" type="text" id="AppUrlInAddApp" name="Url" 
                            style="width:175px;"
                            data-options="required:true" /></td>
            </tr>
            <tr>
                <td><label for="DescriptionInAddApp">简介：</label></td>
                <td><input class="easyui-validatebox" type="text" id="DescriptionInAddApp" name="Description" 
                            style="width:175px;"/></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align:center; padding-top:10px">
                    <a href="javascript:void(0)" 
                        class="easyui-linkbutton" 
                        id="btnAddApp" 
                        iconcls="icon-ok" >确定</a>
                    <a href="javascript:void(0)" 
                        class="easyui-linkbutton close_dlg"
                        dlg="dlgAddApp"
                        iconcls="icon-cancel">关闭</a>
                </td>
            </tr>
        </table>
    </form>
</div>

<div id="dlgUpdateApp" class="easyui-dialog" style="width:320px;height:180px; padding:10px 20px"
		closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <form id="UpdateApp" method="post">
        <table>
            <tr>
                <th colspan="2" >修改应用</th>
            </tr>              
            <tr>
                <td><label for="AppNameInUpdateApp">应用名称：</label></td>
                <td><input class="easyui-validatebox" type="text" id="AppNameInUpdateApp" name="AppName" 
                            style="width:175px;"
                            data-options="required:true" /></td>
            </tr>
            <tr>
                <td><label for="AppUrlInUpdateApp">URL地址：</label></td>
                <td><input class="easyui-validatebox" type="text" id="AppUrlInUpdateApp" name="Url" 
                            style="width:175px;"
                            data-options="required:true" /></td>
            </tr>
            <tr>
                <td><label for="DescriptionInUpdateApp">简介：</label></td>
                <td><input class="easyui-validatebox" type="text" id="DescriptionInUpdateApp" name="Description" 
                            style="width:175px;"/></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align:center; padding-top:10px">
                    <a href="javascript:void(0)" 
                        class="easyui-linkbutton" 
                        id="btnUpdateApp" 
                        iconcls="icon-ok" >确定</a>
                    <a href="javascript:void(0)" 
                        class="easyui-linkbutton close_dlg"
                        dlg="dlgUpdateApp"
                        iconcls="icon-cancel">关闭</a>
                </td>
            </tr>
        </table>

        <input type="hidden" id="AppIdInUpdateApp" name="AppId" />
    </form>
</div>

<div id="dlgSetAppRole" class="easyui-dialog" style="width:600px;height:480px;padding:10px 20px;text-align:center;"
		closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <table id="roles" style="height:390px;margin-left:auto;margin-right:auto;"></table>
    <div style="margin-top:10px;margin-left:auto;margin-right:auto;">
        <a href="javascript:void(0)" 
            class="easyui-linkbutton" 
            id="btnSetAppRole" 
            iconcls="icon-ok" >确定</a>
        <a href="javascript:void(0)" 
            class="easyui-linkbutton close_dlg"
            dlg="dlgSetAppRole"
            iconcls="icon-cancel">关闭</a>
    </div>
    <input type="hidden" name="A_ID" id="A_IDInSetAppRole" />
</div>


<%-- JavaScript --%>

<script type="text/javascript">
    function dlgAddApp() {
        this.id = '#dlgAddApp';
        this.dlg = $(this.id);

        this.ShowDialog = function () {
            this.dlg.find('input').val('');
            this.dlg.dialog('open');
        };

        this.CloseDialog = function () {
            this.dlg.dialog('close');
        };

        this.dlg.find('.close_dlg').click(function () {
            $('#dlgAddApp').dialog('close');
        });

        this.dlg.dialog({
            title: '添加应用'
        });

        this.dlg.find('#btnAddApp').click(function () {
            var validate = $('#AddApp').form('validate');
            if (validate == false) {
                return false;
            }

            $.post('/admin/ajax/addApp.ashx',
                $('#AddApp').serialize(),
                function (data) {
                    if (data.state == 'ok') {
                        $.messager.alert('提示', '添加成功');

                        $('#dlgAddApp').dialog('close');
                        $('#apps').datagrid('reload');
                    }
                    else {
                        $.messager.alert('提示', '添加失败' + data.msg ? ", " + data.msg : "");
                    }
                }, 'json');
        });
    }
</script>

<script type="text/javascript">
    function dlgUpdateApp() {
        this.id = '#dlgUpdateApp';
        this.dlg = $(this.id);

        this.ShowDialog = function (row) {
            this.dlg.find('input').val('');
            this.dlg.find('#AppIdInUpdateApp').val(row.id);
            this.dlg.find('#AppNameInUpdateApp').val(row.name);
            this.dlg.find('#AppUrlInUpdateApp').val(row.url);
            this.dlg.find('#DescriptionInUpdateApp').val(row.description);
            this.dlg.dialog('open');
        };

        this.CloseDialog = function () {
            this.dlg.dialog('close');
        };

        this.dlg.find('.close_dlg').click(function () {
            $('#dlgUpdateApp').dialog('close');
        });

        this.dlg.dialog({
            title: '修改应用'
        });

        this.dlg.find('#btnUpdateApp').click(function () {
            var validate = $('#UpdateApp').form('validate');
            if (validate == false) {
                return false;
            }

            $.post('/admin/ajax/updateApp.ashx',
                $('#UpdateApp').serialize(),
                function (data) {
                    if (data.state == 'ok') {
                        $.messager.alert('提示', '修改成功');

                        $('#dlgUpdateApp').dialog('close');
                        $('#apps').datagrid('reload');
                    }
                    else {
                        $.messager.alert('提示', '修改失败' + data.msg ? ", " + data.msg : "");
                    }
                }, 'json');
        });
    }
</script>

<script type="text/javascript">
    function onDelete() {
        var row = $('#apps').datagrid('getSelected');
        if (row) {
            $.messager.confirm(
                "提示", "是否删除当前选择项?",
                function (r) {
                    if (r == true) {
                        $.post(
                            '/admin/ajax/deleteApp.ashx',
                            {
                                Id: row.id
                            },
                            function (data) {
                                if (data.state == 'ok') {
                                    $.messager.alert('提示', '删除成功');
                                    $('#apps').datagrid('reload');
                                }
                                else
                                    $.messager.alert('提示', '删除失败');
                            }, 'json');
                    }
                });
        }
        else {
            $.messager.alert('提示', '请先选择需要删除的应用');
        }
    }
</script>

<script type="text/ecmascript">
    function dlgSetAppRole() {
        this.id = '#dlgSetAppRole';
        this.dlg = $(this.id);

        this.ShowDialog = function (row) {
            $('#roles').datagrid('loadData', []);
            $('#A_IDInSetAppRole').val('');

            $('#roles').datagrid('options').url = '/admin/ajax/grid/getAllRolesByApp.ashx';
            $('#roles').datagrid('reload', { id: row.id });
            $('#A_IDInSetAppRole').val(row.id);

            this.dlg.dialog('open');
        }

        this.dlg.dialog({ title: '分配应用所需要的权限' });
        this.dlg.find('.close_dlg').click(function () {
            $('#dlgSetAppRole').dialog('close');
        });

        var grid = $('#roles');
        grid.datagrid(
            {
                title: '应用角色权限',
                idField: 'id',
                columns: [[
                    { field: 'id', hidden: true },
                    { field: 'state', checkbox: true },
                    { field: 'cname', width: 100, title: '角色名称' },
                    { field: 'ename', width: 100, title: '角色英文名' },
                    { field: 'description', width: 200, title: '角色说明' }
                ]],

                onLoadSuccess: function (data) {
                    grid.datagrid('clearChecked');
                    for (var i = 0; i < data.rows.length; i++) {
                        if (data.rows[i].state == true)
                            grid.datagrid('selectRecord', data.rows[i].id);
                    }
                },
            });

        $(this.dlg).find('#btnSetAppRole').bind('click', function () {
            var grid = $('#roles');
            var rows = grid.datagrid('getChecked');
            var data = {
                a_id: parseInt($('#A_IDInSetAppRole').val()),
                r_ids: new Array()
            };
            for (var i = 0; i < rows.length; ++i) {
                data.r_ids[i] = parseInt(rows[i].id);
            }

            $.post('/admin/ajax/setAppRoles.ashx',
            { data: JSON.stringify(data) },
            function (d) {
                if (d.state == 'ok') {
                    $.messager.alert('提示', '分配成功');
                    $('#dlgSetAppRole').dialog('close');
                }
                else {
                    $.messager.alert('提示', '分配失败');
                }
            }, 'json');
        });
    }
</script>
    
<script type="text/javascript">
    $(document).ready(function () {
        var apps = $('#apps');
        var add = new dlgAddApp();
        var update = new dlgUpdateApp();
        var setRoleApp = new dlgSetAppRole();

        var doAction = function (action, msg) {
            var row = apps.datagrid('getSelected');
            if (row) {
                action(row);
            }
            else {
                $.messager.alert(msg);
            }
        }

        apps.datagrid({
            idField: 'id',
            rownumbers: true,
            url: '/admin/ajax/grid/getAllApps.ashx',
            singleSelect: true,

            columns: [[
                { field: 'id', hidden: true },
                { title: '名称', field: 'name', width: 180 },
                { title: 'URL地址', field: 'url', width: 180 },
                { title: '简介', field: 'description', width: 200 },
                { title: '状态', field: 'state', formatter: function (v, r, i) { if (v == 1) return '启用'; else return '禁用'; } }
            ]],

            toolbar: [{
                id: '_btnAddApp',
                text: '添加',
                iconCls: 'icon-add',
                handler: function () {
                    add.ShowDialog();
                }
            }, '-', {
                id: '_btnUpdateApp',
                text: '修改',
                iconCls: 'icon-cut',
                handler: function () {
                    var func = function (row) {
                        update.ShowDialog(row);
                    }

                    doAction(func, '请先选择要修改的应用');
                }
            }, '-', {
                id: '_btnDeleteApp',
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    onDelete();
                }
            }, '-', {
                id: '_btnEnableApp',
                text: '启用/禁用',
                iconCls: 'icon-edit',
                handler: function () {
                    var row = apps.datagrid('getSelected');
                    if (row) {
                        var msg = row.state == 0 ?
                            '是否启用当前选择应用?' :
                            '是否禁用当前选择应用?';
                        $.messager.confirm(
                            '提示',
                            msg,
                            function (r) {
                                if (r == false) {
                                    return;
                                }

                                $.post('/admin/ajax/enableApp.ashx',
                                        { id: row.id, state: row.state },
                                        function (data) {
                                            if (data.state == 'ok') {
                                                $.messager.alert('提示', '操作成功');
                                            }
                                            else {
                                                $.messager.alert('提示', '操作失败');
                                            }

                                            apps.datagrid('reload');
                                        }, 'json');
                            });
                    }
                    else {
                        $.messager.alert('提示', '请选择应用');
                    }
                }
            }, '-', {
                id: '_btnSetRole',
                text: '分配权限',
                iconCls: 'icon-edit',
                handler: function () {
                    var func = function (row) {
                        setRoleApp.ShowDialog(row);
                    }

                    doAction(func, '请先选择应用');
                }
            }]
        });            
    });
</script>
